package mathive.client.panels;

import mathive.client.ClientUser;
import mathive.client.PanelHandler;
import mathive.client.UserHandler;
import mathive.client.UserService;
import mathive.client.UserServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserListPanel {
	private static final UserServiceAsync SERVICE = GWT.create(UserService.class);

	VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel testsPanel = new VerticalPanel();
	private ScrollPanel scrollPanel = new ScrollPanel(testsPanel);
	private String[] userList;
	
	public UserListPanel(){
		scrollPanel.setSize("450px", "300px");

	}
	
	public Widget getPanel(){
		mainPanel.clear();
		initialize();
		return mainPanel;
	}
	
	public void initialize() {
		SERVICE.getAllUsers(new GetAllUsersResponse());
		
		mainPanel.add(new Label("Loading..."));
	}
	
	private class GetAllUsersResponse implements AsyncCallback<String[]> {
		@Override
		public void onFailure(Throwable caught) {
		}

		@Override
		public void onSuccess(String[] result) {
			testsPanel.clear();
			mainPanel.clear();
			mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			mainPanel.add(scrollPanel);
			
			userList = new String[result.length];
			for (int i = 0; i < result.length; i++) {
				testsPanel.add(new ListUser(result[i], i));
			}
			
		}
	}
	
	private class UserUpdatedResponse implements AsyncCallback<Boolean> {
		@Override
		public void onFailure(Throwable caught) {
		}

		@Override
		public void onSuccess(Boolean result) {
			
			if (result.booleanValue()) {
				PanelHandler.showUserList();
			} else {
				
			}
		}
	}
	
	private class ListUser extends VerticalPanel {
		private HorizontalPanel first = new HorizontalPanel();
		private HorizontalPanel second = new HorizontalPanel();
		
		private int indexx;
		private Button deleteButton;
		private Button changePassButton;
		private Button changePassButton2;
		TextBox password = new TextBox();
		private ListBox typeList = new ListBox();
		
		boolean changeShow = false;
		
		public ListUser(String info, int index) {
			indexx = index;
			deleteButton = new Button("Delete");
			changePassButton = new Button("Change Password");
			changePassButton2 = new Button("Change");
			
			typeList.addItem("User");
			typeList.addItem("Test user");
			typeList.addItem("Admin");
			
			final String name = info.substring(0, info.indexOf(' '));
			
			final int usertype = Integer.parseInt(info.substring(info.indexOf(' ') + 1));
			typeList.setSelectedIndex(usertype);
			
			userList[index] = name;
			
			first.add(new Label(name));
			first.add(typeList);
			first.add(deleteButton);
			first.add(changePassButton);
			
			second.add(password);
			second.add(changePassButton2);
			
			add(first);
			deleteButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					if (UserHandler.singleton.isOnline())
						SERVICE.deleteUser(UserHandler.singleton.getSessionID(), userList[indexx], new UserUpdatedResponse());
				}
			});
			
			changePassButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					if (changeShow) {
						remove(second);
						changeShow = false;
					} else {
						add(second);
						setCellHorizontalAlignment(second, HasHorizontalAlignment.ALIGN_RIGHT);
						changeShow = true;
					}
				}
			});
			
			changePassButton2.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					SERVICE.AdminChangePassword(UserHandler.singleton.getSessionID(), userList[indexx], password.getText(), new UserUpdatedResponse());
				}
			});
			
			typeList.addChangeHandler(new ChangeHandler() {
				public void onChange(ChangeEvent event) {
					if (typeList.getItemText(typeList.getSelectedIndex()).equals("User")) {
						SERVICE.changeUserType(UserHandler.singleton.getSessionID(), userList[indexx], ClientUser.USER, new UserUpdatedResponse());
					} else if (typeList.getItemText(typeList.getSelectedIndex()).equals("Test user")) {
						SERVICE.changeUserType(UserHandler.singleton.getSessionID(), userList[indexx], ClientUser.TEST_USER, new UserUpdatedResponse());
					} else {
						SERVICE.changeUserType(UserHandler.singleton.getSessionID(), userList[indexx], ClientUser.ADMIN, new UserUpdatedResponse());
					}
				}
			});
		}
	}
}
